
package api.download;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;
import java.lang.ref.SoftReference;

/**
 * Create by WilburLi wcbkanaz@163.com on 2016/12/14
 */
public abstract class API_DownloadMgr {

    public abstract void startDLActivity(Context context);
    public abstract int getDownloadNoFinishCount(Context context);
    public abstract void addNewDownload(String url, String appName, String downloadPath, String packName, String icon, boolean autoResume, RequestCallBack<File> callback, Context context);
    public abstract void addNewDownload(String url, String appName, String downloadPath, String packName, String icon, boolean autoResume, RequestCallBack<File> callback, DL_TYPE type, Context context);
    public abstract void addNewDownload(String url, String appName, String downloadPath, String packName, String icon, boolean autoResume, RequestCallBack<File> callback, DL_TYPE type, Context context,Activity activity);
    public abstract Intent getDlIntent(Context context);

    public static String clazz = "api.download.DlManager_API";

    private static SoftReference<API_DownloadMgr> sf;


    public synchronized static API_DownloadMgr getInstance() {
        if (sf == null || sf.get() == null) {
            Object o = null;
            try {
                o = Class.forName(clazz).newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            if (o != null) {
                sf = new SoftReference<API_DownloadMgr>((API_DownloadMgr) o);
                return (API_DownloadMgr) o;
            }
        }
        return sf == null ? null : sf.get();
    }

    /****
     * 下载类型
     */
    public enum DL_TYPE {
        FRAME(0), NOTI_TAB1(1), NOTI_TAB2(2), APP_TAB1(3), APP_TAB2(4), BANNER(5), WEBVIEW(6), FOLDER(
                7), EXIT_GLITTER(8), DLMGR_RE(9),Flash(10),H5BOX(11),OTHER(99);
        private int value = 0;

        DL_TYPE(int value) {
            this.value = value;
        }

        public static DL_TYPE valueOf(int value) {
            switch (value) {
                case 0:
                    return FRAME;
                case 1:
                    return NOTI_TAB1;
                case 2:
                    return NOTI_TAB2;
                case 3:
                    return APP_TAB1;
                case 4:
                    return APP_TAB2;
                case 5:
                    return BANNER;
                case 6:
                    return WEBVIEW;
                case 7:
                    return FOLDER;
                case 8:
                    return EXIT_GLITTER;
                case 9:
                    return DLMGR_RE;
                case 10:
                    return Flash;
                case 11:
                    return H5BOX;
                case 99:
                    return OTHER;
                default:
                    return APP_TAB1;
            }
        }

        public int value() {
            return this.value;
        }
    }

    public static DL_TYPE logicDlTYPE(boolean isFFrame, boolean isFFromBanner,
                                      boolean isTab2, boolean isFFromNoti, boolean isFWebView) {
        if (isFFromBanner) return DL_TYPE.BANNER;
        if (isFFrame) return DL_TYPE.FRAME;
        if (isFWebView) return DL_TYPE.WEBVIEW;
        if (isTab2 && isFFromNoti) return DL_TYPE.NOTI_TAB2;
        if (!isTab2 && isFFromNoti) return DL_TYPE.NOTI_TAB1;
        if (isTab2 && !isFFromNoti) return DL_TYPE.APP_TAB2;
        if (!isTab2 && !isFFromNoti) return DL_TYPE.APP_TAB1;
        return DL_TYPE.APP_TAB1;
    }
}
